/* jshint esversion: 6 */
$(document).ready(function() {
    ifLoged();
    ajaxXHR('GET', url + "v1/account/detail", function(data) {
        $('.modal-body').html('获取信息成功！');
        $('.modal').modal('show');
        if (data.data.account.avatar) {
            var img = $('<img class="localImage pa" src="' + url_file + data.data.account.avatar + '" >');
            $('#localImage').append(img);
        }
        $('#userName').attr('value', data.data.account.name);
        $('#cellPhoneNum').html(data.data.account.phone);
        if (data.data.account.weipayId) {
            $('#wechatBonding').disable().html("已经绑定过微信").css('background', '#fff');
        }
    }, {
        token: localStorage.APISHoteltoken,
    });
    var formData = new FormData();
    $('#fileHiden').on('change', function() {
        var file = $('#fileHiden')[0].files[0];
        formData.append('token', localStorage.APISHoteltoken);
        formData.append('type', 'avatar');
        var filereader = new FileReader();
        filereader.onload = function(e) {
            if (file.size > 3 * 1024 * 1024) {
                $('.modal-body').html('上传图片大小不得超过3M!');
                $('.modal').modal('show');
                return;
            }
            var srcpath = e.target.result;
            showPreviewImage(srcpath);
        };
        filereader.readAsDataURL(file);
        formData.append('img', file);

        function showPreviewImage(src) {
            // console.log(src);
            $('#localImage img').remove();
            var img = $('<img class="localImage pa" src="' + src + '" >');
            $('#localImage').append(img);
        }
    })
    $('#wechatBonding').on('click', function() {
        $('.wechatContainer').show();
        $('#wechatBond').attr('src', url + "v1/account/wei/bind?token=" + localStorage.APISHoteltoken); // 将获取到的html元素插入
        $('.wechatContainer').on('click', function() {
            if (confirm("请确认是否离开!")) {
                $(this).hide();
            }
        });
    });
});
$('#submit').on('click', function() {
    $.ajax({
            url: url + 'v1/upload/picture',
            type: 'POST',
            cache: false,
            data: formData,
            // beforeSend: function(request) {
            //     request.setRequestHeader("Content-Type", "multipart/form-data");
            // },
            xhrFields: {
                withCredentials: true // 允许跨域名储存和访问cookie
            },
            processData: false,
            contentType: false
                // "multipart/form-data"
        })
        .done(function(data) {
            if (data.code != "success") {
                $('.modal-body').html(data.message);
                $('.modal').modal('show');
            } else {
                $('.modal-body').html('上传图片成功');
                $('.modal').modal('show');
                localStorage.APISHotelAvatar = data.data.picturePath;
                // 调取信息更改接口
            }
        })
        .fail(function() {
            $('.modal-body').html('上传图片失败');
            $('.modal').modal('show');
        });
    ajaxXHR('post', url + 'v1/account/detail/change', function(data) {
        console.log(data);
        $('.modal-body').html('更改信息成功!');
        $('.modal').modal('show');
    }, {
        token: localStorage.APISHoteltoken,
        avatarPath: "avatar/068c5f5a8218125c8416a775bc8efc8d.jpeg",
        name: $('#userName').val(),
    });
});